package com.umeox.ota.ui.activity;

import androidx.lifecycle.LifecycleObserver;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.umeox.ota.mvvm.IViewModel;
import com.umeox.ota.mvvm.ViewModelFactory;
import com.umeox.ota.utils.ActivityCollector;
import com.umeox.ota.utils.NotifyUtils;

/**
 * @author fighter_lee
 * @date 2018/7/24
 */
public abstract class BaseActivity<DB extends ViewDataBinding, VM extends IViewModel> extends AppCompatActivity {

    protected final String TAG = this.getClass().getName();

    protected ViewModelFactory mViewModelFactory;

    /**
     * ViewDataBinding
     */
    protected DB mBinding;

    /**
     * instance in subclass; 自动销毁
     */
    protected VM mViewModel;

    public boolean isShowing = false;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this, getClass());
        //设置DataBinding
        mViewModelFactory = ViewModelFactory.getInstance(this.getApplication());
        genViewModel();
        mBinding = DataBindingUtil.setContentView(this, genLayoutId(savedInstanceState));
        initSystemBar();
        initData(savedInstanceState);
        if (mViewModel != null) {
            //添加LifecycleObserver
            if (observerLifeCycle()) {
                getLifecycle().addObserver((LifecycleObserver) mViewModel);
            }
        }
    }

    protected boolean observerLifeCycle() {
        return true;
    }

    /**
     * 生成对应的 VM
     */
    protected abstract void genViewModel();

    /**
     * @param savedInstanceState
     * @return Layout的XML文件
     */
    protected abstract int genLayoutId(Bundle savedInstanceState);

    /**
     * 初始化数据
     *
     * @param savedInstanceState
     */
    protected abstract void initData(Bundle savedInstanceState);

    @Override
    protected void onDestroy() {
        ActivityCollector.removeActivity(this);
        this.mBinding = null;
        //移除LifecycleObserver
        if (mViewModel != null) {
            getLifecycle().removeObserver((LifecycleObserver) mViewModel);
        }
        this.mViewModel = null;
        isShowing = false;
        super.onDestroy();
    }

    private void initSystemBar() {
        if (translucentStatusBar()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                //                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
                localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
            }
        }
    }

    /**
     * 子类可以重写决定是否使用透明状态栏
     */
    protected boolean translucentStatusBar() {
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        isShowing = true;
        NotifyUtils.removeNotify();
    }

    @Override
    protected void onStop() {
        super.onStop();
        isShowing = false;
    }

    @Override
    public void finish() {
        super.finish();
    }
}
